import TWEEN, { Tween } from '@tweenjs/tween.js';
import { ITweenOption } from 'src/types/animateType'

type Actions = {
  action: Tween<any>
  resolve: (arg0: boolean) => void
  complete: () => Promise<void>
}

export default class {
  props: Record<string, any> | null = null
  actions: Actions[] = []
  constructor(props: Record<string, any>) {
    this.props = props
  }
  to(duration: number, props: Record<string, number>, opts?: ITweenOption) {
    if (!this.props) return this

    const obj = {
      resolve: () => { },
      complete: (): Promise<void> => new Promise((resolve) => obj.resolve = resolve),
      action: new TWEEN.Tween(this.props)
        .to(props, duration)
        .onUpdate(opts?.onUpdate)
        .onComplete((param) => {
          opts?.onComplete && opts.onComplete(param)

          obj.resolve()
        })
    }
    this.actions.push(obj)
    return this
  }
  // by(props: Record<string, number>, opts: ITweenOption) {
  //   if (!this.props) return this
  // }
  start() {
    (async () => {
      for (let i = 0; i < this.actions.length; i++) {
        this.actions[i].action.start()
        await this.actions[i].complete()
      }
    })()
  }
}
